/**
 * Copyright 2010 Terrestrial Ecosystem Research Network, licensed under the Apache
 * License, Version 2.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or
 * agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and
 * limitations under the License.
 */

package au.org.tern.ecoinformatics.oai.provider;

import java.text.SimpleDateFormat;
import java.util.Iterator;
import java.util.Properties;

import ORG.oclc.oai.server.catalog.RecordFactory;
import ORG.oclc.oai.server.verb.CannotDisseminateFormatException;
import au.org.tern.ecoinformatics.oai.provider.crosswalk.Crosswalk;
import au.org.tern.ecoinformatics.oai.provider.service.IdentifierService;
import au.org.tern.ecoinformatics.oai.provider.util.StaticApplicationContextHelper;


public class BasicOaiRecordFactory extends RecordFactory {

	private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
	
	public BasicOaiRecordFactory(Properties properties) {
		super(properties);
	}

	public BasicOaiRecordFactory() {
		this(new Properties());
	}

	
	@Override
	public String fromOAIIdentifier(String identifier) {

		return getIdentifierServiceFromAppContext().extractInternalIdentifier(identifier);
	}

	
	private IdentifierService getIdentifierServiceFromAppContext() {

		return (IdentifierService)StaticApplicationContextHelper.getApplicationContext().getBean("identifierService");
	}

	@Override
	public String quickCreate(Object nativeItem, String schemaURL, String metadataPrefix)
			throws IllegalArgumentException, CannotDisseminateFormatException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getOAIIdentifier(Object nativeItem) {
		return getIdentifierServiceFromAppContext().createOaiIdentifier(nativeItem);
	}

	@Override
	public String getDatestamp(Object nativeItem) {

		return getCrosswalkFromSpringAppContext().getDatestamp(nativeItem);
	}

	@Override
	public Iterator getSetSpecs(Object nativeItem) throws IllegalArgumentException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean isDeleted(Object nativeItem) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public Iterator getAbouts(Object nativeItem) {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * Grabs a reference to the Crosswalk singleton from the Spring applicationContext
	 * @return
	 */
	private Crosswalk getCrosswalkFromSpringAppContext() {
		
		return (Crosswalk) StaticApplicationContextHelper.getApplicationContext().getBean("crosswalk");
	}
}
